﻿using AutoMail.Log.QuartzLog;
using AutoMail.QuartzJobs.Jobs.Utils;
using AutoMail.Tools.EmailTools;
using Quartz;
using Quartz.Job;
using System.Net;
using System.Net.Mail;

namespace AutoMail.QuartzJobs.Jobs
{
    [PersistJobDataAfterExecution]
    public class AnniversaryMailSendJob : SendMailJob
    {
        public const string PropertyAnniversaryYears = "anniversaryYears";
        public const string PropertyStaffName = "curStaffName";
        public static readonly string PropertyEnableSsl = "EnableSsl";
        public static readonly string JobGroup = "AnniversaryComeToCompanyMailJob";
        public static readonly string JobBuildDescription = "Sending an Anniversary email every year to a staff at the date for whom already worked at company exactly reached whole number year";

        public AnniversaryMailSendJob()
        {

        }

        public override async Task Execute(IJobExecutionContext context)
         {
            QuartzLogger.LogUserInfo("[AnniversaryMailSendJob] Execute Start......");

            var anniversaryYears = context.MergedJobDataMap.GetInt(PropertyAnniversaryYears);
            var mailBody = context.MergedJobDataMap.GetString(PropertyMessage);

            // If the value is a file path, then it must be the Email template file.
            var isHtmlFile = File.Exists(mailBody);
            MailInfo mailInfo = new MailInfo
            {
                MailMessage = new MailMessage(context.MergedJobDataMap.GetString(PropertySender)
                    , context.MergedJobDataMap.GetString(PropertyRecipient))
                {
                    Subject = EmailSubject.Anniversary.GetString(anniversaryValue: anniversaryYears),
                    Body = isHtmlFile ? GetMailTemplate(mailBody, anniversaryYears, context.MergedJobDataMap.GetString(PropertyStaffName)) : mailBody,
                    IsBodyHtml = isHtmlFile
                },
                SmtpHost = context.MergedJobDataMap.GetString(PropertySmtpHost),
                SmtpPort = context.MergedJobDataMap.GetInt(PropertySmtpPort),
                SmtpUserName = context.MergedJobDataMap.GetString(PropertyUsername),
                SmtpPassword = context.MergedJobDataMap.GetString(PropertyPassword),
            };

            QuartzLogger.LogUserInfo("[AnniversaryMailSendJob] Finished Mail Send preparation......");

            await Send(mailInfo, context.MergedJobDataMap.GetBoolean(PropertyEnableSsl));
            QuartzLogger.LogUserInfo("[AnniversaryMailSendJob] Finished Mail Sending......");

            // Plus 1 on anniversaryYears value, for next aniversary mail sending.
            context.JobDetail.JobDataMap.Put(PropertyAnniversaryYears, (++anniversaryYears).ToString());
        }

        private string GetMailTemplate(string mailTemplatePath, int anniversaryYears, string staffName)
        {
            using (StreamReader rd = new StreamReader(mailTemplatePath))
            {
                return rd.ReadToEnd()
                    .Replace("{#base64PicAnniversaryMail#}", PictureBase64Generating.GetAnniversaryMailTemplatePictureBase64String1(staffName, anniversaryYears.ToString(), DateTime.Now.ToShortDateString()) ?? "");
            }
        }

        protected new async Task Send(MailInfo mailInfo, bool enableSsl = true)
        {
            SmtpClient client = new SmtpClient(mailInfo.SmtpHost, mailInfo.SmtpPort ?? 0);
            client.Credentials = new NetworkCredential(mailInfo.SmtpUserName, mailInfo.SmtpPassword);
            client.EnableSsl = enableSsl;
            await client.SendMailAsync(mailInfo.MailMessage);

            QuartzLogger.LogUserInfo("[AnniversaryMailSendJob] ------****** sending Sucessfully ******------");
        }
    }
}
